//用于页面组件通信
import { defineStore } from 'pinia'
import { reactive} from 'vue'
import router from '@/router';
import { useUserStore} from './user';
import {ElMessage} from 'element-plus';
import SecureLS from 'secure-ls';
const ls = new SecureLS({ isCompression: false, encryptionSecret: '38c31684-d00d-30dc-82e0-fad9eec46d1d' });
const st = {
  setItem(key, value) {
    ls.set(key, value)
  },
  getItem(key){
    return ls.get(key)
  }
}
export const usePageStore = defineStore('page', {
    state: () => ({
      //系统tab栏
      menuToggle:0,//0为打开1为折叠
      activeMenuInfo:[{index:"0",name:"首页",path:0,parent_name:"管理系统"}],//创建响应式数组
      activeItem:reactive({index:"0",name:"首页",path:0,parent_name:"管理系统"}),//当前选中的菜单项
      //禁言框变量
      isShowMuteBox:false,
      uid:null,
      days:1,
      type:'lock'
    }),
    getters: {

    },
    actions: {
      //选中标签
     selectTab(target){
      let hasPermission = useUserStore().auth.includes(target);
      if(hasPermission){
        router.push({ name: target });
        this.activeItem = {...this.activeMenuInfo.find(item=>item.index === target)};
      }
     },
    removeTab(targetName){
      let tmp = this.activeMenuInfo;
      tmp = tmp.filter(item => item.index !== targetName);
      this.activeMenuInfo = tmp;
      this.selectTab(tmp[tmp.length-1].index);
  },
     //打开禁言框
     openMuteBox(val,index,type){
        this.isShowMuteBox = val;
        this.uid = index;
        this.type = type;
      },
      resetPage(){
        this.$reset();
        ls.removeAll();
      }
    },
    persist: {
      key:'_ks',
      paths: ['menuToggle'],
      storage:st
    }
  })